package com.zh.zuul.filter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.micrometer.core.instrument.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 所有请求统一异常处理
 * @Author: Liard
 * @Create in: 2020/6/6 14:21
 * @Version: 1.0.1
 */
@Component
public class ReqFilter extends ZuulFilter {
    private static final Logger log = LoggerFactory.getLogger(ReqFilter.class);

    @Override
    public String filterType() {
        return FilterConstants.ERROR_TYPE;
    }

    @Override
    public int filterOrder() {
        return -1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        HttpServletResponse response = ctx.getResponse();
        String message = "";
        try {
            Throwable e = (Exception) ctx.get("throwable");
            Throwable re = getOriginException(e);
            if (re instanceof java.net.ConnectException) {
                message = "Real Service Connection refused";
                log.warn("uri:{},error:{}", request.getRequestURI(), re.getMessage());
            } else if (re instanceof java.net.SocketTimeoutException) {
                message = "Real Service Timeout";
                log.warn("uri:{},error:{}", request.getRequestURI(), re.getMessage());
            } else if (re instanceof com.netflix.client.ClientException) {
                message = re.getMessage();
                log.warn("uri:{},error:{}", request.getRequestURI(), re.getMessage());
            } else {
                log.warn("Error during filtering", e);
            }
            response.sendError(502, message);
        } catch (Exception e) {
            try {
                response.sendError(500, e.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    private Throwable getOriginException(Throwable e) {
        e = e.getCause();
        while (e.getCause() != null) {
            e = e.getCause();
        }
        return e;
    }
}
